package com.cn.jdia.dc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cn.jdia.dc.model.Dbconfig;
import com.cn.jdia.dc.model.Mqconfig;
import com.cn.jdia.dc.model.Serverconfig;
import com.cn.jdia.dc.model.TopoInstance;
import com.cn.jdia.dc.model.TopoLink;
import com.cn.jdia.dc.model.TopoNode;
import com.cn.jdia.dc.service.IDbconfigService;
import com.cn.jdia.dc.service.IMqconfigService;
import com.cn.jdia.dc.service.IServerconfigService;
import com.cn.jdia.dc.service.ITopoInstanceService;
import com.cn.jdia.dc.service.ITopoLinkService;
import com.cn.jdia.dc.service.ITopoNodeService;
import com.google.gson.Gson;

@Controller
public class TopologyMonitorController {

	@Resource
	private ITopoInstanceService topoInstanceService;

	@Resource
	private ITopoNodeService topoNodeService;

	@Resource
	private ITopoLinkService topoLinkService;

	@Resource
	private IServerconfigService serverconfigService;

	@Resource
	private IDbconfigService dbconfigService;

	@Resource
	private IMqconfigService mqconfigService;

	@RequestMapping("/topologydrawer")
	@ResponseBody
	public ModelAndView topologydrawer() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("topologydrawer");

		// cache the list required info
		List<Serverconfig> serverConfigList = serverconfigService.getAllServerConfig();
		List<String> serverList = new ArrayList<>();
		for (Serverconfig svconfig : serverConfigList) {
			serverList.add(svconfig.getServername());
		}

		List<Dbconfig> dbConfigList = dbconfigService.queryAll();
		List<String> dbList = new ArrayList<>();
		for (Dbconfig dbconfig : dbConfigList) {
			dbList.add(dbconfig.getServername());
		}

		List<Mqconfig> mqconfigList = mqconfigService.getAllConfig();
		List<String> mqList = new ArrayList<>();
		for (Mqconfig mqconfig : mqconfigList) {
			mqList.add(mqconfig.getServername());
		}

		Gson gson = new Gson();

		mav.addObject("svlist", gson.toJson(serverList));
		mav.addObject("dblist", gson.toJson(dbList));
		mav.addObject("mqlist", gson.toJson(mqList));

		return mav;
	}

	@RequestMapping(value = "/topomonitor")
	@ResponseBody
	public ModelAndView topoMonitor(HttpServletRequest request) {

		String project = "NONE";
		if (request.getParameter("project") != null) {
			project = request.getParameter("project");
		}
		// System.out.println("project:["+project+"]");

		ModelAndView mav = new ModelAndView();
		mav.setViewName("topomonitor");

		List<String> proList = new ArrayList<>();
		List<TopoInstance> topoList = topoInstanceService.queryAllTopoInstance();
		for (TopoInstance instance : topoList) {
			proList.add(instance.getProject());
		}

		// cache the list required info
		List<Serverconfig> serverConfigList = serverconfigService.getAllServerConfig();
		List<String> serverList = new ArrayList<>();
		for (Serverconfig svconfig : serverConfigList) {
			serverList.add(svconfig.getServername());
		}

		List<Dbconfig> dbConfigList = dbconfigService.queryAll();
		List<String> dbList = new ArrayList<>();
		for (Dbconfig dbconfig : dbConfigList) {
			dbList.add(dbconfig.getServername());
		}

		List<Mqconfig> mqconfigList = mqconfigService.getAllConfig();
		List<String> mqList = new ArrayList<>();
		for (Mqconfig mqconfig : mqconfigList) {
			mqList.add(mqconfig.getServername());
		}

		Gson gson = new Gson();

		mav.addObject("list", proList);
		mav.addObject("svlist", gson.toJson(serverList));
		mav.addObject("dblist", gson.toJson(dbList));
		mav.addObject("mqlist", gson.toJson(mqList));

		mav.addObject("project", project);

		return mav;
	}

	@RequestMapping(value = "/toposelect", produces = "application/text;charset=utf-8")
	@ResponseBody
	public String selectTopo(String topoid) {
		TopoInstance instance = topoInstanceService.getTopoInstanceByProject(topoid);
		String instanceid = instance.getInstanceid();
		// System.out.println(topoid + " : " + instanceid);

		Gson gson = new Gson();

		Map<String, Object> retMap = new HashMap<>();
		retMap.put("class", instance.getDclass());
		retMap.put("linkFromPortIdProperty", instance.getLinkfromportidproperty());
		retMap.put("linkToPortIdProperty", instance.getLinktoportidproperty());

		List<TopoNode> nodeList = topoNodeService.getTopoNode(instanceid);
		List<Map<String, Object>> nodeDataArray = new ArrayList<>();
		Map<String, Object> temp;

		List<Map<String, Object>> nodeAttrArray = new ArrayList<>();
		Map<String, Object> attrMap;

		for (TopoNode toponode : nodeList) {
			temp = new HashMap<>();
			temp.put("key", toponode.getNodekey());
			temp.put("text", toponode.getNodetext());
			temp.put("loc", toponode.getLoca());
			temp.put("status", toponode.getBak3());

			if ((toponode.getBak1() == null)) {
				temp.put("type", "NUM");
			} else {
				if (toponode.getBak3() != null) {
					temp.put("type", toponode.getBak1() + toponode.getBak3());
				} else {
					temp.put("type", toponode.getBak1());
				}
			}

			if (!("Node".equals(toponode.getCategory()))) {
				temp.put("category", toponode.getCategory());
			}

			nodeDataArray.add(temp);

			attrMap = new HashMap<>();
			attrMap.put("nodetext", toponode.getNodetext());
			attrMap.put("nodekey", toponode.getNodekey());
			attrMap.put("nodetype", toponode.getBak1());
			attrMap.put("nodesource", toponode.getBak2());
			attrMap.put("nodestatus", toponode.getBak3());
			nodeAttrArray.add(attrMap);
		}
		retMap.put("nodeDataArray", nodeDataArray);
		retMap.put("nodeAttrArray", nodeAttrArray);

		List<TopoLink> linkList = topoLinkService.getTopoLinkByProject(instanceid);
		List<Map<String, Object>> linkDataArray = new ArrayList<>();
		for (TopoLink topoLink : linkList) {
			temp = new HashMap<>();
			temp.put("from", topoLink.getLinkfrom());
			temp.put("to", topoLink.getLinkto());
			temp.put("fromPort", topoLink.getFromport());
			temp.put("toPort", topoLink.getToport());

			linkDataArray.add(temp);
		}
		retMap.put("linkDataArray", linkDataArray);

		// System.out.println(gson.toJson(retMap));

		return gson.toJson(retMap);
	}

}
